package org.example.bookstore.service.impl;

import org.example.bookstore.custom.ResultVO;
import org.example.bookstore.mapper.BookMapper;
import org.example.bookstore.pojo.DO.Book;
import org.example.bookstore.pojo.DTO.StaticAllData;
import org.example.bookstore.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;
    /*
     * 根据书的类型 小说，励志
     * 来查询商品
     * */
    @Override
    public List<Book> selectBooks(String type, Integer page, Integer pageSize) {
        int numFrom =(page-1)*pageSize;
        return bookMapper.selectByType(type,numFrom,pageSize);
    }
    /*
     * 查询书本类型总数
     * */
    @Override
    public Integer selectGoodsSum(String type) {
        return bookMapper.selectNum(type);
    }

    /*
     * 根据书本id来查询
     * */
    @Override
    public Book selectBookById(Integer id) {
        return bookMapper.selectById(id);
    }
    /*
     * 根本书本根据数组查询商品列表
     * */
    @Override
    public List<Book> selectBooksByIds(List<Integer> ids) {
        return bookMapper.selectByIds(ids);
    }
    /*
     * 根据书本id来删除
     * */
    @Override
    public ResultVO<?>  deleteBookById(Integer id) {
        int i=bookMapper.deleteById(id);
        if (i==0){
            return ResultVO.error();
        }
        return  ResultVO.success("删除成功");
    }

    /*
     * 模糊搜索
     * */
    @Override
    public List<Book> SearchGoods(String content) {
        return bookMapper.selectByLikeContent(content);
    }

    /*
     * 管理员权限
     * 新增书本
     * */
    @Override
    public ResultVO<?> addBook(Book book) {
        int i=bookMapper.addBook(book);
        if (i==0){
            return ResultVO.error();
        }
        return  ResultVO.success("添加成功");
    }
    /*
     * 管理员权限
     * 更新书本信息
     * */
    @Override
    public ResultVO<?> updateBook(Book book) {
        int i=bookMapper.updateBook(book);
        if (i==0){
            return ResultVO.error();
        }
        return  ResultVO.success("添加成功");
    }

    @Override
    public ResultVO<?> selectDates() {
        int allBook = bookMapper.selectAllBook();
        int allUser = bookMapper.selectAllUser();
        int allSell = bookMapper.selectAllByOrderType(1);
        int allBorrow = bookMapper.selectAllByOrderType(2);
        // 创建Statistics对象
        StaticAllData stats = new StaticAllData(allBook, allUser, allSell, allBorrow);
        // 返回封装了Statistics对象的ResultVO
        return ResultVO.success(stats);
    }
}
